package com.cryann.mqttserver.mqttProcess;

import com.cryann.mqttserver.modle.MqttMessageStore;
import com.cryann.mqttserver.modle.mqtt.Message.MqttPingRes;
import com.cryann.mqttserver.service.MqttAccessor;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author cRyann
 * @date 2018/12/18 0018 11:17
 * @description:
 */
@Component
@Slf4j
@ProcessType(MqttMessageType.PINGREQ)
public class PingResp implements BasicProcess<MqttMessage> {

    @Autowired
    MqttMessageStore mqttMessageStore;
    @Autowired
    MqttAccessor mqttAccessor;

    @Override
    public void process(Channel ctx, MqttMessage msg) {
        MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, msg.fixedHeader().qosLevel(), false, 0);
        // 更新Accessor 状态，保持Online标记
        mqttAccessor.updateAccessorByChannel(ctx);

        MqttPingRes mqttPingRes = new MqttPingRes(mqttFixedHeader);
        ctx.writeAndFlush(mqttPingRes);
    }
}
